home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / examples / C / x15c.c < prev    next >
C/C++ Source or Header  |  1994-06-30  |  7KB  |  292 lines

  1. /* $Id: x15c.c,v 1.6 1994/06/30 17:57:56 mjl Exp $
  2.  * $Log: x15c.c,v $
  3.  * Revision 1.6  1994/06/30  17:57:56  mjl
  4.  * All C example programs: made another pass to eliminate warnings when using
  5.  * gcc -Wall.  Lots of cleaning up: got rid of includes of math.h or string.h
  6.  * (now included by plplot.h), eliminated redundant casts, put in more
  7.  * uniform comments, and other minor changes.
  8.  *
  9.  * Revision 1.5  1994/04/08  12:07:55  mjl
  10.  * Changed to only pattern shade.
  11.  *
  12.  * Revision 1.4  1994/03/30  07:22:00  mjl
  13.  * Changes to all C example programs: special handling for malloc re: header
  14.  * files eliminated, include of stdio.h and stdlib.h eliminated (now done
  15.  * by plplot.h), include of "plplot.h" changed to <plplot.h> to enable
  16.  * simpler builds by the general user, some cleaning up also.
  17.  *
  18.  * Revision 1.3  1993/12/06  22:38:16  mjl
  19.  * Added #include <stdio.h> to pick up definition of NULL under SunOS.
  20. */
  21.  
  22. /*    x15c.c
  23.  
  24.     Shade plot demo.
  25.  
  26.     Maurice LeBrun
  27.     IFS, University of Texas at Austin
  28.     31 Aug 1993
  29. */
  30.  
  31. #include <plplot.h>
  32.  
  33. #define XPTS    35        /* Data points in x */
  34. #define YPTS    46        /* Datat points in y */
  35.  
  36. PLFLT z[XPTS][YPTS], zmin, zmax;
  37.  
  38. /* Utility macros */
  39.  
  40. #ifndef PI
  41. #define PI    3.1415926535897932384
  42. #endif
  43. #ifndef MAX
  44. #define MAX(a,b)    (((a) > (b)) ? (a) : (b))
  45. #endif
  46. #ifndef MIN
  47. #define MIN(a,b)    (((a) < (b)) ? (a) : (b))
  48. #endif
  49.  
  50. /* Function prototypes */
  51.  
  52. static void    plot1        (void);
  53. static void    plot2        (void);
  54. static void    f2mnmx        (PLFLT *, PLINT, PLINT, PLFLT *, PLFLT *);
  55. static void    cmap1_init1    (void);
  56. static void    cmap1_init2    (void);
  57.  
  58. /*----------------------------------------------------------------------*\
  59.  * main
  60.  *
  61.  * Does a variety of shade plots.
  62. \*----------------------------------------------------------------------*/
  63.  
  64. int
  65. main(int argc, char *argv[])
  66. {
  67.     int i, j;
  68.     PLFLT xx, yy;
  69.  
  70. /* Parse and process command line arguments */
  71.  
  72.     (void) plParseInternalOpts(&argc, argv, PL_PARSE_FULL);
  73.  
  74. /* Set up color map 0 */
  75. /*
  76.     plscmap0n(3);
  77.     */
  78. /* Set up color map 1 */
  79.  
  80.     cmap1_init2();
  81.  
  82. /* Initialize plplot */
  83.  
  84.     plinit();
  85.  
  86. /* Set up data array */
  87.  
  88.     for (i = 0; i < XPTS; i++) {
  89.     xx = (double) (i - (XPTS / 2)) / (double) (XPTS / 2);
  90.     for (j = 0; j < YPTS; j++) {
  91.         yy = (double) (j - (YPTS / 2)) / (double) (YPTS / 2) - 1.0;
  92.  
  93.         z[i][j] = xx*xx - yy*yy + (xx - yy)/(xx*xx+yy*yy + 0.1);
  94.     }
  95.     }
  96.     f2mnmx(&z[0][0], XPTS, YPTS, &zmin, &zmax);
  97.  
  98.     plot1();
  99.     plot2();
  100.  
  101.     plend();
  102.     exit(0);
  103. }
  104.  
  105. /*----------------------------------------------------------------------*\
  106.  * cmap1_init1
  107.  *
  108.  * Initializes color map 1 in HLS space.
  109. \*----------------------------------------------------------------------*/
  110.  
  111. static void
  112. cmap1_init1(void)
  113. {
  114.     PLFLT i[4], h[4], l[4], s[4];
  115.  
  116.     i[0] = 0;        /* left boundary */
  117.     i[1] = 0.45;    /* just before center */
  118.     i[2] = 0.55;    /* just after center */
  119.     i[3] = 1;        /* right boundary */
  120.  
  121.     h[0] = 260;        /* hue -- low: blue-violet */
  122.     h[1] = 260;        /* only change as we go over vertex */
  123.     h[2] = 20;        /* hue -- high: red */
  124.     h[3] = 20;        /* keep fixed */
  125.  
  126. #if 1
  127.     l[0] = 0.5;        /* lightness -- low */
  128.     l[1] = 0.0;        /* lightness -- center */
  129.     l[2] = 0.0;        /* lightness -- center */
  130.     l[3] = 0.5;        /* lightness -- high */
  131. #else
  132.     plscolbg(255,255,255);
  133.     l[0] = 0.5;        /* lightness -- low */
  134.     l[1] = 1.0;        /* lightness -- center */
  135.     l[2] = 1.0;        /* lightness -- center */
  136.     l[3] = 0.5;        /* lightness -- high */
  137. #endif
  138.     s[0] = 1;        /* maximum saturation */
  139.     s[1] = 1;        /* maximum saturation */
  140.     s[2] = 1;        /* maximum saturation */
  141.     s[3] = 1;        /* maximum saturation */
  142.  
  143.     c_plscmap1l(0, 4, i, h, l, s);
  144. }
  145.  
  146. /*----------------------------------------------------------------------*\
  147.  * cmap1_init2
  148.  *
  149.  * Initializes color map 1 in HLS space.
  150. \*----------------------------------------------------------------------*/
  151.  
  152. static void
  153. cmap1_init2(void)
  154. {
  155.     PLFLT i[4], h[4], l[4], s[4];
  156.  
  157.     i[0] = 0;        /* left boundary */
  158.     i[1] = 0.45;    /* just before center */
  159.     i[2] = 0.55;    /* just after center */
  160.     i[3] = 1;        /* right boundary */
  161.  
  162.     h[0] = 260;        /* hue -- low: blue-violet */
  163.     h[1] = 260;        /* only change as we go over vertex */
  164.     h[2] = 20;        /* hue -- high: red */
  165.     h[3] = 20;        /* keep fixed */
  166.  
  167. #if 1
  168.     l[0] = 0.6;        /* lightness -- low */
  169.     l[1] = 0.0;        /* lightness -- center */
  170.     l[2] = 0.0;        /* lightness -- center */
  171.     l[3] = 0.6;        /* lightness -- high */
  172. #else
  173.     plscolbg(255,255,255);
  174.     l[0] = 0.5;        /* lightness -- low */
  175.     l[1] = 1.0;        /* lightness -- center */
  176.     l[2] = 1.0;        /* lightness -- center */
  177.     l[3] = 0.5;        /* lightness -- high */
  178. #endif
  179.     s[0] = 1;        /* saturation -- low */
  180.     s[1] = 0.5;        /* saturation -- center */
  181.     s[2] = 0.5;        /* saturation -- center */
  182.     s[3] = 1;        /* saturation -- high */
  183.  
  184.     c_plscmap1l(0, 4, i, h, l, s);
  185. }
  186.  
  187. /*----------------------------------------------------------------------*\
  188.  * plot1
  189.  *
  190.  * Illustrates a single shaded region.
  191. \*----------------------------------------------------------------------*/
  192.  
  193. static void 
  194. plot1(void)
  195. {
  196.     PLFLT shade_min, shade_max, sh_color;
  197.     PLINT sh_cmap = 0, sh_width;
  198.     PLINT min_color = 0, min_width = 0, max_color = 0, max_width = 0;
  199.  
  200.     pladv(0);
  201.     plvpor(0.1, 0.9, 0.1, 0.9);
  202.     plwind(-1.0, 1.0, -1.0, 1.0);
  203.  
  204. /* Plot using identity transform */
  205.  
  206.     shade_min = zmin + (zmax-zmin)*0.4;
  207.     shade_max = zmin + (zmax-zmin)*0.6;
  208.     sh_color = 7;
  209.     sh_width = 2;
  210.     min_color = 9;
  211.     max_color = 2;
  212.     min_width = 2;
  213.     max_width = 2;
  214.  
  215.     plpsty(8);
  216.     plshade1(&z[0][0], XPTS, YPTS, NULL, -1., 1., -1., 1., 
  217.          shade_min, shade_max, 
  218.          sh_cmap, sh_color, sh_width,
  219.          min_color, min_width, max_color, max_width,
  220.          plfill, 1, NULL, NULL);
  221.  
  222.     plcol(1);
  223.     plbox("bcnst", 0.0, 0, "bcnstv", 0.0, 0);
  224.     plcol(2);
  225.     pllab("distance", "altitude", "Bogon flux");
  226. }
  227.  
  228. /*----------------------------------------------------------------------*\
  229.  * plot2
  230.  *
  231.  * Illustrates multiple adjacent shaded regions, using different fill
  232.  * patterns for each region. 
  233. \*----------------------------------------------------------------------*/
  234.  
  235. static void 
  236. plot2(void)
  237. {
  238.     PLFLT shade_min, shade_max, sh_color;
  239.     PLINT sh_cmap = 0, sh_width;
  240.     PLINT min_color = 0, min_width = 0, max_color = 0, max_width = 0;
  241.     int i;
  242.  
  243.     pladv(0);
  244.     plvpor(0.1, 0.9, 0.1, 0.9);
  245.     plwind(-1.0, 1.0, -1.0, 1.0);
  246.  
  247. /* Plot using identity transform */
  248.     
  249.     for (i = 0; i < 10; i++) {
  250.     shade_min = zmin + (zmax - zmin) * i / 10.0;
  251.     shade_max = zmin + (zmax - zmin) * (i +1) / 10.0;
  252.     sh_color = i+6;
  253.     sh_width = 2;
  254.     plpsty((i + 2) % 8 + 1);
  255.  
  256.     plshade1(&z[0][0], XPTS, YPTS, NULL, -1., 1., -1., 1., 
  257.          shade_min, shade_max, 
  258.          sh_cmap, sh_color, sh_width,
  259.          min_color, min_width, max_color, max_width,
  260.          plfill, 1, NULL, NULL);
  261.     }
  262.  
  263.     plcol(1);
  264.     plbox("bcnst", 0.0, 0, "bcnstv", 0.0, 0);
  265.     plcol(2);
  266.     pllab("distance", "altitude", "Bogon flux");
  267. }
  268.  
  269. /*----------------------------------------------------------------------*\
  270.  * f2mnmx
  271.  *
  272.  * Returns min & max of input 2d array.
  273. \*----------------------------------------------------------------------*/
  274.  
  275. #define F(a,b) (f[a*ny+b])
  276.  
  277. static void
  278. f2mnmx(PLFLT *f, PLINT nx, PLINT ny, PLFLT *fmin, PLFLT *fmax)
  279. {
  280.     int i, j;
  281.  
  282.     *fmax = F(0,0);
  283.     *fmin = *fmax;
  284.  
  285.     for (i = 0; i < nx; i++) {
  286.     for (j = 0; j < ny; j++) {
  287.             *fmax = MAX(*fmax, F(i, j));
  288.             *fmin = MIN(*fmin, F(i, j));
  289.     }
  290.     }
  291. }
  292.